package oa.hleast.xswl.service.place.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import oa.hleast.common.web.exception.BizException;
import oa.hleast.xswl.common.enums.ScenesTypeEnum;
import oa.hleast.xswl.mapper.place.ScenesScourMapper;
import oa.hleast.xswl.pojo.domain.place.CmsScenesScour;
import oa.hleast.xswl.service.place.IScenesScourService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@AllArgsConstructor
@Service
@Transactional
public class ScenesScourServiceImpl extends ServiceImpl<ScenesScourMapper, CmsScenesScour> implements IScenesScourService {
    @Override
    public boolean add(CmsScenesScour scenesScour) {
        return this.save(scenesScour);
    }

    @Override
    public boolean update(CmsScenesScour scenesScour) {
        return this.updateById(scenesScour);
    }

    @Override
    public CmsScenesScour detail(Long id) {
        return this.getById(id);
    }

    @Override
    public boolean updateByScenesId(Long scenesId, Integer startRating, String scenesType) {

        CmsScenesScour scenesScour = getByScenesId(scenesId, scenesType);

        if (ObjectUtils.isNotEmpty(scenesScour)) {
            //更新分数
            boolean update = this.update(new LambdaUpdateWrapper<CmsScenesScour>()
                    .setSql("total_scour_num = total_scour_num +" + startRating)
                    .setSql("join_num = join_num +" + 1)
                    .eq(CmsScenesScour::getId, scenesScour.getId()));
            log.info("update = {}", update);

            return update;
        }

        // 正常情况下并不会走到这，一般在创建场景时就已经创建好了评分表
        CmsScenesScour createScenesScour = CmsScenesScour.builder()
                .scenesId(scenesId)
                .type(ScenesTypeEnum.PLACE.name())
                .totalScourNum(startRating)
                .joinNum(1)
                .build();
        boolean save = this.save(createScenesScour);
        log.info("save = {}", save);

        return save;
    }

    @Override
    public CmsScenesScour getByScenesId(Long scenesId, String scenesType) {
        LambdaQueryWrapper<CmsScenesScour> queryWrapper = new LambdaQueryWrapper<CmsScenesScour>()
                .eq(CmsScenesScour::getScenesId, scenesId)
                .eq(CmsScenesScour::getType, scenesType)
                .orderByDesc(CmsScenesScour::getGmtCreate);
        List<CmsScenesScour> list = this.list(queryWrapper);

        if (list.size() > 1) {
            throw new BizException("数据异常，请稍后重试");
        }

        if (CollectionUtils.isEmpty(list)) {
            log.info("list = {}", list);
            return null;
        }

        return list.get(0);
    }
}
